//给定一个二叉树，原地将它展开为一个单链表。 
//
// 
//
// 例如，给定二叉树 
//
//     1
//   / \
//  2   5
// / \   \
//3   4   6 
//
// 将其展开为： 
//
// 1
// \
//  2
//   \
//    3
//     \
//      4
//       \
//        5
//         \
//          6 
// Related Topics 树 深度优先搜索 
// 👍 685 👎 0

package leetcode.editor.cn;
//Java：二叉树展开为链表
public class FlattenBinaryTreeToLinkedList{
    public static void main(String[] args) {
        Solution solution = new FlattenBinaryTreeToLinkedList().new Solution();
        
    }
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public void flatten(TreeNode root) {
        if ( root == null ){
            return;
        }
        //从底部开始拉平
        flatten( root.left );
        flatten( root.right );

        //后序遍历
        //左右子树拉平成一条链表
        TreeNode left = root.left;
        TreeNode right = root.right;

        //左子树作为右子树
        root.left = null;
        root.right = left;

        //将原先的右子树接到现在右子树的末尾
        TreeNode p = root;
        //把节点p定位到现在右子树的末尾
        while ( p.right != null ){
            p = p.right;
        }
        //将原先右子树接到现在右子树的末尾
        p.right = right;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
